﻿<?php
// Prevent PHP from stopping the script after 30 sec
set_time_limit(0);
//removes notices
error_reporting(E_ALL ^ E_NOTICE);

//get the latest used forum-id
$xmldata = "C:\\wamp\\www\\data.xml";
$ctimer = 10;
$time = 30;
$length = 300;

$file = file_get_contents($xmldata,NULL,NULL,0,20);
preg_match('@[0-9]+@',$file,$file);
$id = $file[0];

//get the latest used gw2blog-url
$file = file_get_contents($xmldata);
preg_match('@<gw2blog>[^<]+@',$file,$file);
$gw2blog = str_replace("<gw2blog>","",$file[0]);

//get the latest used gw2news-url
$file = file_get_contents($xmldata);
preg_match('@<gw2news>[^<]+@',$file,$file);
$gw2news = str_replace("<gw2news>","",$file[0]);

//get the latest used gw2Sverigenews-url
$file = file_get_contents($xmldata);
preg_match('@<gw2Snews>[^<]+@',$file,$file);
$gw2Snews = str_replace("<gw2Snews>","",$file[0]);

//get the latest used gw2guru-url
$file = file_get_contents($xmldata);
preg_match('@<gw2Gnews>[^<]+@',$file,$file);
$gw2Gnews = str_replace("<gw2Gnews>","",$file[0]);
flush(); @ob_flush();  // outputs some crap thats in the way
while(1){
	flush(); @ob_flush();  // make sure that all output is sent in real-time
	
	$fp = @fsockopen("www.guildwars2guru.com", 80, $errno, $errstr, $ctimer); // open connection to guildwars2.com
	if (!$fp){
		echo "$errstr ($errno)\n";
	}else {
		//echo $id."\n";
		$out = "GET /feed/ HTTP/1.1\r\n";
		$out .= "Host: www.guildwars2guru.com\r\n";
		$out .= "User-Agent: Jacqli/0.2 jacqli.mir@gmail.com\r\n";
		$out .= "Connection: Close\r\n\r\n";
		fwrite($fp, $out);
		$gw2 = "";
		while (!feof($fp)) {
			$gw2 .= fgets($fp, 128);
		}
	
		if(preg_match('@<guid isPermaLink="false">[^<]*@',$gw2,$matches)){
			$url = str_replace('<guid isPermaLink="false">',"",$matches[0]);
			if($url == $gw2Gnews || $gw2Gnews == ""){
				//do nothing
			}else{
				$file_contents = file_get_contents($xmldata);
				$fh = fopen($xmldata, "w");
				$file_contents = str_replace("<gw2Gnews>".$gw2Gnews."</gw2Gnews>","<gw2Gnews>".$url."</gw2Gnews>",$file_contents);
				fwrite($fh, $file_contents);
				fclose($fh);
				$gw2Gnews = $url;
		
				preg_match('@<item>\s*<title>[^<]*@',$gw2,$matches);
				$title = html_entity_decode(preg_replace('@<item>\s*<title>@',"",$matches[0]),ENT_QUOTES,"UTF-8"); //get the title of the news
		
				preg_match('@<description><!\[CDATA\[[^\[\]]+(\[...\])?@',$gw2,$matches);
				$text = str_replace('<description><![CDATA[',"",  $matches[0]); //get some text from the news
				$text = html_entity_decode(preg_replace("@<[^>]*>@","",substr($text,0,$length)),ENT_QUOTES,"UTF-8");

				echo "PRIVMSG #gw2sverige :[GW2Guru] Nyhet: ".$title." - ".$text."[...] - ".$url."\n";
				flush(); @ob_flush();  // make sure that all output is sent in real-time
			}
		}
		fclose($fp);
	}
	sleep($time);
	
	$fp = @fsockopen("gw2sverige.se", 80, $errno, $errstr, $ctimer); //open connection to arena.net
	if (!$fp){
		echo "$errstr ($errno)\n";
	}else {
		//echo $id."\n";
		$out = "GET /feed/ HTTP/1.1\r\n";
		$out .= "Host: gw2sverige.se\r\n";
		$out .= "User-Agent: Jacqli/0.2 jacqli.mir@gmail.com\r\n";
		$out .= "Connection: Close\r\n\r\n";
		fwrite($fp, $out);
		$gw2 = "";
		while (!feof($fp)) {
			$gw2 .= fgets($fp, 128);
		}
		preg_match('@</title><link>[^<]*@',$gw2,$matches);
		$url = str_replace('</title><link>',"",$matches[0]);
		if($url == $gw2Snews || $gw2Snews == ""){
			//do nothing
		}else{		
			if(preg_match('@<item>\s*<title>[^<]*@',$gw2,$matches)){ //get the title, which we also saves
				$title = html_entity_decode(preg_replace('@<item>\s*<title>@',"",$matches[0]),ENT_QUOTES,"UTF-8");
		
				preg_match('@<description><!\[CDATA\[[^\[]*@',$gw2,$matches);
				$text = html_entity_decode(substr(str_replace('<description><![CDATA[',"",  $matches[0]),0,$length),ENT_QUOTES,"UTF-8");
		
				$file_contents = file_get_contents($xmldata);
				$fh = fopen($xmldata, "w");
				$file_contents = str_replace("<gw2Snews>".$gw2Snews."</gw2Snews>","<gw2Snews>".$url."</gw2Snews>",$file_contents);
				fwrite($fh, $file_contents);
				fclose($fh);
				$gw2Snews = $url;
		
				echo "PRIVMSG #gw2sverige :[GW2Sverige] Nyhet: ".$title." - ".$text."[...] - ".$url."\n";
				flush(); @ob_flush();  // make sure that all output is sent in real-time
			}
		}
		fclose($fp);
	}
	flush(); @ob_flush();  // make sure that all output is sent in real-time
	sleep($time);	
	
	for($i = 1; $i < 10; $i++){
		$fp = @fsockopen("forum.gw2sverige.se", 80, $errno, $errstr, $ctimer); //open connection to gw2sverige's forum
		if (!$fp){
			echo "$errstr ($errno)\n";
			$i = 4711;
		}else{		
			$out = "GET /viewtopic.php?id=".($id+$i)." HTTP/1.1\r\n";
			$out .= "Host: forum.gw2sverige.se\r\n";
			$out .= "User-Agent: Jacqli/0.2 jacqli.mir@gmail.com\r\n";
			$out .= "Connection: Close\r\n\r\n";
			fwrite($fp, $out);
			$forum = "";
			while (!feof($fp)) {
				$forum .= fgets($fp, 128);
			}
			if(strpos($forum,"<p>Ett fel uppstod") !== false){
				//do nothing
			}else{
				//echo $forum;
				$id += $i;
				if(preg_match('@<title>[^/]*/[^/]*/@', $forum, $matches)){ //makes sure we get a title, which we also saves
					$matches[0] = str_replace("<title>","",$matches[0]);
					preg_match('@[^/]*@',$matches[0],$titel); //get the title of the topic
					$matches[0] = str_replace($titel,"",$matches[0]);
					preg_match('@/[^/]*@',$matches[0],$kat); //get the category the thread isin
					$kat[0][0] = ' ';
					$kat[0] = html_entity_decode($kat[0],ENT_QUOTES,"UTF-8");
					$titel[0] = preg_replace('@\(Sida\s*[0-9]+\)@','',$titel[0]); //get the title without any extra doodads
					$titel[0] = html_entity_decode($titel[0],ENT_QUOTES,"UTF-8");
					echo "PRIVMSG #gw2sverige :[GW2Sverige] Ny tråd:".$kat[0]."/ ".$titel[0]."- http://forum.gw2sverige.se/viewtopic.php?id=".$id."\n";
					flush(); @ob_flush();  // make sure that all output is sent in real-time
		
					$file_contents = file_get_contents($xmldata); //get the data from a xml-file
					$fh = fopen($xmldata, "w");
					$file_contents = str_replace("<forumid>".($id-$i)."</forumid>","<forumid>".$id."</forumid>",$file_contents); //update it with new info
					$i = 170;
					fwrite($fh, $file_contents); //write new content to xml-file
				}
				fclose($fh);		
			}
		}
	}
	flush(); @ob_flush();  // make sure that all output is sent in real-time
	sleep($time);
	
	$fp = @fsockopen("www.arena.net", 80, $errno, $errstr, $ctimer); //open connection to arena.net
	if (!$fp){
		echo "$errstr ($errno)\n";
	}else {
		//echo $id."\n";
		$out = "GET /blog/feed HTTP/1.1\r\n";
		$out .= "Host: www.arena.net\r\n";
		$out .= "User-Agent: Jacqli/0.2 jacqli.mir@gmail.com\r\n";
		$out .= "Connection: Close\r\n\r\n";
		fwrite($fp, $out);
		$gw2 = "";
		while (!feof($fp)) {
			$gw2 .= fgets($fp, 128);
		}
		preg_match('@isPermaLink="false">[^<]*@',$gw2,$matches);
		$url = str_replace('isPermaLink="false">',"",$matches[0]);
		if($url == $gw2blog || $gw2blog == ""){
			//do nothing
		}else{		
			if(preg_match('@<item>\s*<title>[^<]*@',$gw2,$matches)){
				$title = html_entity_decode(preg_replace('@<item>\s*<title>@',"",$matches[0]),ENT_QUOTES,"UTF-8");
		
				preg_match('@</p>[^<]*@',$gw2,$matches);
				$text = html_entity_decode(substr(str_replace('</p>',"",  $matches[0]),0,$length),ENT_QUOTES,"UTF-8");
		
				$file_contents = file_get_contents($xmldata);
				$fh = fopen($xmldata, "w");
				$file_contents = str_replace("<gw2blog>".$gw2blog."</gw2blog>","<gw2blog>".$url."</gw2blog>",$file_contents);
				fwrite($fh, $file_contents);
				fclose($fh);
				$gw2blog = $url;
		
				echo "PRIVMSG #gw2sverige :[GW2Blog] Ny blogpost: ".$title." - ".$text." - ".$url."\n";
				flush(); @ob_flush();  // make sure that all output is sent in real-time
			}
		}
		fclose($fp);
	}
	flush(); @ob_flush();  // make sure that all output is sent in real-time
	sleep($time);	
	
	$fp = @fsockopen("www.guildwars2.com", 80, $errno, $errstr, $ctimer); // open connection to guildwars2.com
	if (!$fp){
		echo "$errstr ($errno)\n";
	}else {
		//echo $id."\n";
		$out = "GET /en/rss.xml HTTP/1.1\r\n";
		$out .= "Host: www.guildwars2.com\r\n";
		$out .= "User-Agent: Jacqli/0.2 jacqli.mir@gmail.com\r\n";
		$out .= "Connection: Close\r\n\r\n";
		fwrite($fp, $out);
		$gw2 = "";
		while (!feof($fp)) {
			$gw2 .= fgets($fp, 128);
		}
	
		if(preg_match('@<guid>[^<]*@',$gw2,$matches)){
			$url = str_replace('<guid>',"",$matches[0]);
			if($url == $gw2news || $gw2news == ""){
				//do nothing
			}else{
				$file_contents = file_get_contents($xmldata);
				$fh = fopen($xmldata, "w");
				$file_contents = str_replace("<gw2news>".$gw2news."</gw2news>","<gw2news>".$url."</gw2news>",$file_contents);
				fwrite($fh, $file_contents);
				fclose($fh);
				$gw2news = $url;
		
				preg_match('@<item>\s*<title>[^<]*@',$gw2,$matches);
				$title = html_entity_decode(preg_replace('@<item>\s*<title>@',"",$matches[0]),ENT_QUOTES,"UTF-8"); //get the title of the news
		
				preg_match('@CDATA\[[^\]]*@',$gw2,$matches);
				$text = str_replace('CDATA[',"",  $matches[0]); //get some text from the news
				$text = substr(html_entity_decode(preg_replace("@<[^>]*>@","",$text),ENT_QUOTES,"UTF-8"),0,$length);
				echo "PRIVMSG #gw2sverige :[GW2News] Nyhet: ".$title." - ".$text."[...] - ".$url."\n";				
				flush(); @ob_flush();  // make sure that all output is sent in real-time
			}
		}
		fclose($fp);
	}
	flush(); @ob_flush();  // make sure that all output is sent in real-time
	sleep($time);	
	echo "one full loop\n";
}

?>